'''
Created on 8 avr. 2013

@author: Nils Amiet
'''

# coding: utf-8

class HTMLTableGenerator:
    
    infrarougeUserURL = "http://www.infrarouge.ch/ir/member-"
    tableBorder = 1
    
    def __init__(self, th=[], rows=[], title="Untitled page", users=None):
        self.th = th
        self.rows = rows
        self.title = title
        self.users = users
        
    def header(self):
        html = "<!DOCTYPE html>"
        html += "\n"
        html += "<html>"
        html += "\n"
        html += "<head>"
        html += "<title>"
        html += str(self.title)
        html += "</title>"
        html += "</head>"
        html += "\n"
        html += "<body>"
        html += "\n"
        return html
        
    def footer(self):
        html = "</body>"
        html += "\n"
        html += "</html>"
        return html
    
    def h1(self, text):
        html = "<h1>" + text + "</h1>"
        return html
    
    def raw(self):
        html = "<table border=\"" + str(self.tableBorder) + "\">"
        html += "\n"
        
        html += "<tr>"
        for heading in self.th:
            html += "<th>" + str(heading) + "</th>"
        html += "</tr>"
        html += "\n"
        
        for row in self.rows:
            html += "<tr>"
            for col in row:
                html += "<td>"
                html += str(col)
                html += "</td>"
                
            html += "</tr>"
            html += "\n"
            
        html += "</table>"
        html += "\n"
        html += "<br />"
        html += "\n"
        
        return html
        
    def __repr__(self):
        html = "<table border=\"" + str(self.tableBorder) + "\">"
        html += "\n"
        
        html += "<tr>"
        for heading in self.th:
            html += "<th>" + str(heading) + "</th>"
        html += "</tr>"
            
        html += "\n"
        
        for row in self.rows:
            html += "<tr>"            
            colID = 0
            for col in row:
                html += "<td>"
                if colID == 0:
                    html += "<a href=\"" + self.infrarougeUserURL + str(col) + "\">"
                html += str(col)
                if colID == 0:
                    html += "</a>"
                html += "</td>"
                
                if colID == 0:
                    html += "<td>"
                    try:
                        userTuple = [user for user in self.users if user[0] == col][0]
                        username = userTuple[1]
                    except:
                        username = "username not found"
                    html += username
                    html += "</td>"
                
                colID += 1
            html += "</tr>"
            html += "\n"
            
        html += "</table>"
        html += "\n"
        html += "<br />"
        html += "\n"
        
        return html
